package wfb.frontend.threads;

import wfb.definitions.CountryDataInterface;
import wfb.definitions.WfbDbInterface;
import wfb.definitions.threadInterfaces.FillLabelInterface;

import javax.swing.*;
import java.awt.*;

/**
 * @author Tobias Peitzsch
 */
public class FillLabelThread implements FillLabelInterface {
	private JLabel label=null;
	private WfbDbInterface wdi=null;
	private int mode = -1;
	private String country=null;
	private String path=null;
	/* (non-Javadoc)
	 * @see wfb.definitions.threadInterfaces.FillLabelInterface#setLabel(javax.swing.JLabel)
	 */
	public void setLabel(JLabel label) {
		this.label=label;
	}
	/* (non-Javadoc)
	 * @see wfb.definitions.threadInterfaces.FillLabelInterface#setPath(java.lang.String)
	 */
	public void setPath(String path) {
		this.path=path;
	}
	/* (non-Javadoc)
	 * @see wfb.definitions.threadInterfaces.FillInterface#setMode(int)
	 */
	public void setMode(int mode) {
		this.mode=mode;
	}
	/* (non-Javadoc)
	 * @see wfb.definitions.threadInterfaces.FillInterface#setWfbDbImpl(wfb.definitions.WfbDbInterface)
	 */
	public void setWfbDbImpl(WfbDbInterface wdi) {
		this.wdi=wdi;
	}
	/* (non-Javadoc)
	 * @see wfb.definitions.threadInterfaces.FillLabelInterface#setCountry(java.lang.String)
	 */
	public void setCountry(String country) {
		this.country=country;
	}
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		if(path==null||country==null||wdi==null||mode<0||label==null) return;
		if(mode == FillLabelInterface.KARTE){
			final Icon icon = ladeKarte(wdi.getCountryData(country));
			EventQueue.invokeLater(new Runnable(){
				public void run(){
					try{
						label.setIcon(icon );
					}
					catch(Exception e){
						label.setText("Fehler beim Laden der Karte!");
					}
				}
			});
		}	
		else if(mode==FillLabelInterface.FLAGGE){
			final Icon icon=ladeFlagge(wdi.getCountryData(country));
			EventQueue.invokeLater(new Runnable(){
				public void run(){
					try{
						label.setIcon(icon );
					}
					catch(Exception e){
						label.setText("Fehler beim Laden der Flagge!");
					}
				}
			});
		}
	}
	private Icon ladeKarte(CountryDataInterface myCountry){
		if(myCountry==null)return null;
		else{
			String ad_hoc = path+"/"+myCountry.getRefMap();
			System.out.println("Der Kartenpfad: "+ad_hoc);
			ImageIcon icon = new ImageIcon(  ad_hoc  );
			Image image = icon.getImage();
			image = image.getScaledInstance(200, 200, Image.SCALE_SMOOTH  ) ;
			icon.setImage(image);
			return icon;		
		}
	}
	private Icon ladeFlagge(CountryDataInterface myCountry){
		if(myCountry==null)return null;
		else{
			String ad_hoc = path+"/"+myCountry.getRefFlag();
			System.out.println("Der Flaggenpfad: "+ad_hoc);
			ImageIcon icon = new ImageIcon(ad_hoc);
			Image image = icon.getImage();
			image = image.getScaledInstance(40, 30, Image.SCALE_SMOOTH  ) ;
			icon.setImage(image);
			return icon;
		}
	}
}
